import BaseUI from "../BaseUI";

const { ccclass, property } = cc._decorator;

@ccclass
export default class qxc_tools extends BaseUI {

    baseUI_onLoad(): void {
        throw new Error("Method not implemented.");
    }
    baseUI_onDestroy(): void {
        throw new Error("Method not implemented.");
    }
    onBtn_click(target: cc.Button, onBtn_name: string): number | void {
        throw new Error("Method not implemented.");
    }
    private static _findInChildren(node: cc.Node, name: string): cc.Node {
        var x = node.getChildByName(name);
        if (x) return x;
        if (node.childrenCount == 0) return null;

        for (var i = 0; i < node.childrenCount; ++i) {
            var tmp = this._findInChildren(node.children[i], name);
            if (tmp) return tmp;
        }
        return null;
    }

    ///////////////// 搜索/查找 子节点 并缓存
    private static m_objects: Map<string, cc.Node> = new Map<string, cc.Node>();
    public static GetGameObject(name: string, node: cc.Node, refind: boolean = false): cc.Node {
        if (!cc.isValid(node)) return null;
        if (!refind) {
            if (this.m_objects && this.m_objects.has(name)) return this.m_objects.get(name);
            if (name == node.name) return node;
        }
        if (name.indexOf("/") != -1) {
            var tmp: cc.Node = cc.find(name, node);
            tmp && this.m_objects.set(name, tmp);
            return tmp;
        }
        else {
            var tmp = this._findInChildren(node, name);
            tmp && this.m_objects.set(name, tmp);
            return tmp;
        }
    }

    public static GetGameComponent<T extends cc.Component>(name: string, type: { prototype: T }, refind: boolean = false, node: cc.Node): T {
        let n: cc.Node = this.GetGameObject(name, node, refind);
        if (n) return n.getComponent(type);
    }

    //获取指定Node或name的child=====QXC 
    public GetChildByNNN(n: cc.Node | string, childName: string): cc.Node {
        var tmp: cc.Node = null;
        if (typeof n == "string") {
            tmp = this.GetGameObject(n)
        } else {
            tmp = n;
        }
        return tmp.getChildByName(childName)
    }
    //获取指定Node或name的component=====QXC 
    public GetComponentByNOrNType<T extends cc.Component>(n: cc.Node | string, childName: string, type: { prototype: T }): T {
        var tmp: cc.Node = null;
        if (typeof n == "string") {
            tmp = this.GetGameObject(n)
        } else {
            tmp = n;
        }
        return tmp.getChildByName(childName).getComponent(type);
    }
    //获取相同类型的children下的同名Component 返回component数组=====QXC   
    public GetChildrenComponentByName<T extends cc.Component>(n: cc.Node | string, childName: string, type: { prototype: T }): Array<T> {
        var tmp: cc.Node = null;
        if (typeof n == "string") {
            tmp = this.GetGameObject(n)
        } else {
            tmp = n;
        }
        var childrenCom = new Array();
        for (let index = 0; index < tmp.children.length; index++) {
            childrenCom[index] = this._GetNodeByChildrenComponent(tmp.children[index], childName, type);
        }
        return childrenCom;
    }

    //递归找指定名的children 返回此child的Component类型=====QXC
    private _GetNodeByChildrenComponent<T extends cc.Component>(node: cc.Node, childName: string, type: { prototype: T }): T {
        if (node.childrenCount == 0) return null;
        if (node.getChildByName(childName) == null) {
            for (let index = 0; index < node.children.length; index++) {
                const element = node.children[index];
                if (this._GetNodeByChildrenComponent(element, childName, type) != null) {
                    return this._GetNodeByChildrenComponent(element, childName, type).getComponent(type);
                }
            }
        } else {
            return node.getChildByName(childName).getComponent(type);
        }
    }


    //递归找指定名的child 返回此child的Node类型=====QXC
    public static GetNodeByChildren(node: cc.Node, childName: string): cc.Node {
        if (node.childrenCount == 0) return null;
        if (node.getChildByName(childName) == null) {
            for (let index = 0; index < node.children.length; index++) {
                const element = node.children[index];
                if (this.GetNodeByChildren(element, childName) != null) {
                    return this.GetNodeByChildren(element, childName);
                }
            }
        } else {
            return node.getChildByName(childName);
        }
    }
    
}


